library(foreign)
library(ggplot2)


####
# Loess curves NAES
# Once data has been coded in Stata, it can be utlized with the follwing code:
# Fiture 1
####
naes <- read.dta(file.choose())

dev.new(width=3.75,height=3.75)
dev.size()

ggplot(naes, aes(date, affect))  +
  stat_smooth(size=.33,color="black",method=loess , se=F) + 
  stat_smooth( method=loess , color = "black", geom = "ribbon", fill = NA, size=.33 , linetype="dotted") + 
	theme_linedraw() +
	scale_x_continuous(breaks = c(-60 , -40, -20 , 0) , labels=c(60, 40, 20 ,0)) +     
	theme(axis.text.y = element_text(angle = 90 , color="black" , hjust=.5)) +
	theme(aspect.ratio=1) + 
	ylab("Candidate Affective Polarization") + xlab("Days Before Election") +
		theme(panel.grid.minor = element_blank()) + 
		theme(panel.grid.major = element_blank()) + 
    theme(panel.border = element_rect(fill=NA, colour = "black", size=.75)) +
	theme(strip.background = element_rect(color="white",fill="white",size=1)) +   
   	theme(strip.text = element_text(color = 'black')) + theme(aspect.ratio=1)


###
# Coefplot for balance
# Figure 2
####
dev.new(width=6,height=3)
dev.size()
library(foreign)
library(ggplot2)
coefplot <-read.csv("/Users/jrthornton/Documents/Work/2. Papers/Current papers/Stability of electoral environment /Data/ceof_balance.csv")
coefplot$model<-as.factor(coef$model)
coefplot$var <- reorder(coefplot$var, coefplot$order)

#####
# GAM for ANES data
# Once data has been coded in Stata, it can be utlized with the follwing code:
# Figure 4
#####
library(gam)
library(foreign)

# Candidates
anes_c <- read.dta(file.choose())

	g <- gam(candidate_affective ~ s(date) + income + education + age + involvement + 
		as.factor(race) + as.factor(married) + as.factor(VCF0112) + calls + as.factor(year) 
		, data= anes_c)

	summary(g)


	plot(g, 
		ann = FALSE,     
		xaxt="n",
	     se = TRUE,
	     ask = TRUE,
	     rug=FALSE
	)

	1 # selects plot
	0 # exits selector

	axis(1, c(-60, -40, -20,0) , labels=c("60" , "40" , "20" , "0"))
	title(ylab="Predicted Candidate Affective\nPolarization (Mean-centered)", 
    	xlab="Days Before Election", cex.lab = 1,
      	line = 2)

# parties          
anes_p <- read.dta(file.choose())

	g <- gam(partisan_affective ~ s(date) + income + education + age + involvement + 
	as.factor(race) + as.factor(married) + as.factor(VCF0112) + calls + as.factor(year) 
	, data= anes_p)
	
	summary(g)

	plot(g, 
		ann = FALSE,     
		xaxt="n",
	     se = TRUE,
	     ask = TRUE,
	     rug=FALSE
	)

	1 # selects plot
	0 # exits selector

axis(1, c(-60, -40, -20,0) , labels=c("60" , "40" , "20" , "0"))

	axis(1, c(-60, -40, -20,0) , labels=c("60" , "40" , "20" , "0"))
	title(ylab="Predicted Party Affective\nPolarization (Mean-centered)", 
    	xlab="Days Before Election", cex.lab = 1,
      	line = 2)
          
# In-party
	g.ci <- gam(in_party ~ s(date) + income + education + age + involvement + 
		as.factor(race) + as.factor(married) + as.factor(VCF0112) + calls + as.factor(year) 
		, data= anes_p)

	summary(g.ci)


	plot(g.ci, 
		ann = FALSE,     
		xaxt="n",
	     se = TRUE,
	     ask = TRUE,
	     rug=FALSE
	)

	1 # selects plot
	0 # exits selector

	axis(1, c(-60, -40, -20,0) , labels=c("60" , "40" , "20" , "0"))
	title(ylab="Predicted In-Party\n Rating (Mean-centered)", 
    	xlab="Days Before Election", cex.lab = 1,
      	line = 2)

# Out-party
	g.co <- gam(out_party ~ s(date) + income + education + age + involvement + 
		as.factor(race) + as.factor(married) + as.factor(VCF0112) + calls + as.factor(year) 
		, data= anes_p)

	summary(g.co)


	plot(g.co, 
		ann = FALSE,     
		xaxt="n",
	     se = TRUE,
	     ask = TRUE,
	     rug=FALSE
	)

	1 # selects plot
	0 # exits selector

	axis(1, c(-60, -40, -20,0) , labels=c("60" , "40" , "20" , "0"))
	title(ylab="Predicted Out-Party\n Rating (Mean-centered)", 
    	xlab="Days Before Election", cex.lab = 1,
      	line = 2)

          



####
# Panel average over time
# Figure 4
####
dev.new(width=6,height=3)
dev.size()
library(foreign)
library(ggplot2)
library(grid)
library(gtable)

panel<-read.csv("/Users/jrthornton/Documents/Work/2. Papers/Current papers/4. Elections and affective polarization/Data/panel combined.csv")

panel$wave <- reorder(panel$wave, panel$wave.order)
#panel$type <- reorder(panel$type, panel$panel.order)

p<-ggplot(panel, aes(x=wave, y= affective)) + 
	geom_linerange(aes(ymin = lower, ymax = upper) , size=.3) +
	geom_point(shape=1,size=1)#,fill="white")
	
g<-p+
facet_grid(type ~ year) +
	theme_linedraw() +
	ylab("Affective Polarization") + 	
	xlab("Wave") +
	theme(axis.text.y = element_text(angle = 90 , color="black" , hjust=.5)) +
	theme(aspect.ratio=1) + 
    theme(panel.grid.minor = element_blank()) +
    theme(panel.grid.major.x = element_blank()) +
	theme(panel.grid.major.y = element_line(colour = "grey88",size=0.33, linetype="solid")) + 
	theme(panel.border = element_rect(fill=NA, colour = "black", size=.667)) +
    theme(strip.background = element_rect(color="white",fill="white",size=1)) + 
  	theme(strip.text = element_text(color = 'black')) +
  	theme(strip.placement = "outside")

grob <- ggplotGrob(g)
idx <- which(grob$layout$name %in% c("panel-2-3"))
for (i in idx) grob$grobs[[i]] <- nullGrob()

idx <- which(grob$layout$name %in% c("axis-b-3" , "axis-b-3"))
grob$layout[idx, c("t", "b")] <- grob$layout[idx, c("t", "b")] - c(2, 2)

grid.newpage()
grid.draw(grob)

####
# Panel average over time by party
# Figure 5
####
dev.new(width=6,height=3)
dev.size()
library(foreign)
library(ggplot2)
library(grid)
library(gtable)
panel<-read.csv("/Users/jrthornton/Documents/Work/2. Papers/Current papers/4. Elections and affective polarization/Data/panel combined by party.csv")

panel$wave <- reorder(panel$wave, panel$wave.order)
#panel$type <- reorder(panel$type, panel$panel.order)
panel$Party<-panel$party
p<-ggplot(panel, aes(x=wave, y= affective)) + 
	geom_linerange(mapping=aes(x=wave,ymin=lower,ymax=upper),size=.33) +
	geom_point(aes(y=affective,group= Party,shape= Party),size=1)
g<-p+
	facet_grid(type ~ year) +
	theme_linedraw() +
	ylab("Affective Polarization") + 	
	xlab("Wave") +
 	theme(axis.text.y = element_text(angle = 90 , color="black" , hjust=.5)) +
	scale_y_continuous(expand = c(0.05, 0.05), breaks=c(0.3,0.5,0.7)) +
	scale_shape_manual(values = c(1, 2)) +
	theme(aspect.ratio=1) + 
    theme(panel.grid.minor = element_blank()) +
    theme(panel.grid.major.x = element_blank()) +
	theme(panel.grid.major.y = element_line(colour = "grey88",size=0.33, linetype="solid")) + 
	theme(panel.border = element_rect(fill=NA, colour = "black", size=.667)) +
    theme(strip.background = element_rect(color="white",fill="white",size=1)) + 
  	theme(strip.text = element_text(color = 'black')) +
  	theme(strip.placement = "outside")

grob <- ggplotGrob(g)
idx <- which(grob$layout$name %in% c("panel-2-3"))
for (i in idx) grob$grobs[[i]] <- nullGrob()

idx <- which(grob$layout$name %in% c("axis-b-3" , "axis-b-3"))
grob$layout[idx, c("t", "b")] <- grob$layout[idx, c("t", "b")] - c(2, 2)

grid.newpage()
grid.draw(grob)

